package com.yn.service.zy.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yn.common.model.zy.TInspectionItemDO;
import com.yn.common.page.BasePage;
import com.yn.common.request.sc.PageParamRequest;
import com.yn.service.dao.zy.TInspectionItemMapper;
import com.yn.service.zy.TInspectionItemService;
import com.yn.service.zy.model.dto.TInspectionItemDTO;
import com.yn.service.zy.model.query.TInspectionItemQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class TInspectionItemServiceImpl extends ServiceImpl<TInspectionItemMapper, TInspectionItemDO> implements TInspectionItemService {

    @Resource
    private TInspectionItemMapper tInspectionItemMapper;

    /**
     * 分页获取检查项信息
     * @param query
     * @param basePage
     * @return
     */
    @Override
    public IPage<TInspectionItemDTO> listPage(TInspectionItemQuery query, BasePage basePage) {
        IPage page = new Page(basePage.getPage(),basePage.getLimit());
        return tInspectionItemMapper.listPage(query,page);
    }

    /**
     * 获取检查项详情
     * @param itemPriceId
     * @return
     */
    @Override
    public TInspectionItemDTO getInfo(Integer itemPriceId) {
        return tInspectionItemMapper.getInfo(itemPriceId);
    }

    @Override
    public BasePage<TInspectionItemDTO> listPage1(TInspectionItemQuery query, PageParamRequest pageRequest) {
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getLimit());
        LambdaQueryWrapper<TInspectionItemDO> lambdaQuery = new LambdaQueryWrapper<>();
        List<TInspectionItemDO> tInspectionItemDOS = baseMapper.selectList(lambdaQuery);
        if(CollUtil.isEmpty(tInspectionItemDOS)){
            return BasePage.restPage(new ArrayList<>());
        }
        BasePage<TInspectionItemDO> pageList = BasePage.restPage(tInspectionItemDOS);
        ArrayList<TInspectionItemDTO> list = new ArrayList<>(tInspectionItemDOS.size());
        for (TInspectionItemDO tInspectionItemDO : tInspectionItemDOS) {
            list.add(TInspectionItemDTO.transform(tInspectionItemDO));
        }
        BasePage<TInspectionItemDTO> page = BasePage.restPage(list);
        BeanUtils.copyProperties(pageList, page,"list");
        return page;
    }
}
